//
//  FriendsListViewController.m
//  模拟-百思不得姐
//
//  Created by 谭浔明 on 16/8/9.
//  Copyright © 2016年 chunyu. All rights reserved.
//

#import "FriendsListViewController.h"
#import "NewListViewController.h"
#import "AddfriendsViewController.h"

@interface FriendsListViewController ()<UITableViewDelegate,UITableViewDataSource,EMContactManagerDelegate>
// 界面的tab
@property (nonatomic, strong) UITableView *tab;
// 好友模型数组
@property (nonatomic, strong) NSMutableArray *array;

@end

@implementation FriendsListViewController

// 懒加载
- (NSMutableArray *)array
{
    if (!_array) {
        _array = [[NSMutableArray alloc] init];
        EMError *error = nil;
        _array = [[[EMClient sharedClient].contactManager getContactsFromServerWithError:&error] mutableCopy];
    }
    return _array;
}
// 从服务器得到最新的好友列表
- (void)getNewUserMessage
{
    [_array removeAllObjects];
    EMError *error = nil;
    self.array = [[[EMClient sharedClient].contactManager getContactsFromServerWithError:&error] mutableCopy];
}
// 创建tab
- (void)createTableView
{
    self.tab = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    [_tab registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    _tab.delegate = self;
    _tab.dataSource = self;
    [self.view addSubview:_tab];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"好友";
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"添加好友" style:(UIBarButtonItemStylePlain) target:self action:@selector(itemClick)];
    self.navigationItem.rightBarButtonItem = item;
    
    [self createTableView];
    
    [[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];
    
    // Do any additional setup after loading the view.
}

- (void)itemClick
{
    AddfriendsViewController *add = [[AddfriendsViewController alloc] init];
    [self.navigationController pushViewController:add animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = [@"好友名称：" stringByAppendingString:self.array[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewListViewController *talk = [[NewListViewController alloc] init];
    talk.str = self.array[indexPath.row];
    [self.navigationController pushViewController:talk animated:YES];
}

// 可以删除好友
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
// 提交删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *username = self.array[indexPath.row];
    [self.array removeObjectAtIndex:indexPath.row];
    [self.tab reloadData];
    [[EMClient sharedClient].contactManager deleteContact:username];
}

// 好友申请调用
- (void)didReceiveFriendInvitationFromUsername:(NSString *)aUsername
                                       message:(NSString *)aMessage
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"好友申请" message:[NSString stringWithFormat:@"%@:%@", aUsername, aMessage] preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"同意" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[EMClient sharedClient].contactManager acceptInvitationForUsername:aUsername];
            [self getNewUserMessage];
            [self.tab reloadData];
        });
    }];
    UIAlertAction *def = [UIAlertAction actionWithTitle:@"拒绝" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [[EMClient sharedClient].contactManager declineInvitationForUsername:aUsername];
    }];
    [alert addAction:sure];
    [alert addAction:def];
    [[self.view getCurrentVC] presentViewController:alert animated:YES completion:nil];
}

// 对方同意添加好友回调
- (void)didReceiveAgreedFromUsername:(NSString *)aUsername
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"用户: %@ 同意了您的好友申请", aUsername] preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        [self getNewUserMessage];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tab reloadData];
        });
    }];
    [alert addAction:sure];
    [[self.view getCurrentVC] presentViewController:alert animated:YES completion:nil];
}

// 对方拒绝好友回调
- (void)didReceiveDeclinedFromUsername:(NSString *)aUsername
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"用户: %@ 拒绝了您的好友申请", aUsername] preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:nil];
    [alert addAction:sure];
    [[self.view getCurrentVC] presentViewController:alert animated:YES completion:nil];
}


@end
